import 'package:flutter/material.dart';
import 'package:flutter_changdian_football/pages/clubs/club_players/club_players_controller.dart';
import 'package:flutter_changdian_football/pages/clubs/clubs_controller.dart';
import 'package:flutter_changdian_football/routers/app_routes.dart';
import 'package:flutter_changdian_football/utils/custom_constant.dart';
import 'package:flutter_changdian_football/utils/pull/pull_smart_refresher.dart';
import 'package:flutter_changdian_football/utils/str_utils.dart';
import 'package:flutter_changdian_football/widget/extended_image_slide_widget.dart';
import 'package:flutter_changdian_football/widget/load_state/load_state_layout.dart';
import 'package:flutter_changdian_football/widget/screenutil/custom_screenutil.dart';
import 'package:get/get.dart';
import 'package:get/route_manager.dart';

/// 俱乐部球员
class ClubPlayersPage extends StatelessWidget {
  const ClubPlayersPage({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        centerTitle: true,
        elevation: 0,
        title: Text(
          '俱乐部球员',
          style: TextStyle(fontSize: 16.sp, color: const Color(0xff333333)),
        ),
        iconTheme: const IconThemeData(
          color: Color(0xff333333)
        ),
        backgroundColor: const Color(0xffF8FAF9 ),
      ),
      body: GetBuilder<ClubsController>(
        builder: (clubsController) {
          return GetBuilder<ClubPlayersController>(
            init: ClubPlayersController(clubId: '${clubsController.clublModel?.clubId ?? ''}'),
            builder: (clubPlayersController) {
              return RefreshWidget<ClubPlayersController>(
                child: ListView.separated(
                  itemCount: clubPlayersController.clubMemberList.length,
                  physics: const BouncingScrollPhysics(),
                  separatorBuilder: (context, index) {
                    return Container(
                      margin: EdgeInsets.only(left: 76.w),
                      width: double.infinity,
                      height: 1.w,
                      color: const Color(0xffF8FAF9),
                    );
                  },
                  itemBuilder: (BuildContext context, int index) {
                    return GestureDetector(
                      behavior: HitTestBehavior.translucent,
                      onTap: () {
                        Get.toNamed(AppRoutes.playerInfo, parameters: {
                          'type': CustomConstant.USER_QY_INFO,
                          'queriedUserId': '${clubPlayersController.clubMemberList[index].userId ?? ''}',
                          'isOperate': (clubsController.clubCenterModel?.userRoleCode == 'CHAIRMAN' || clubsController.clubCenterModel?.userRoleCode == 'ADMINISTRATORS') ? 'true' : 'false',
                          'clubId': '${clubsController.clublModel?.clubId ?? ''}'
                        });
                      },
                      child: Container(
                        padding: EdgeInsets.symmetric(horizontal: 17.w),
                        child: Row(
                          children: [
                            Expanded(
                              flex: 1,
                              child: SizedBox(
                                child: Text(clubPlayersController.clubMemberList[index].clubRoleName ?? '', style: TextStyle(
                                  fontSize: 13.sp, color: StrUtils.roleTextColor(clubPlayersController.clubMemberList[index].clubRoleCode ?? '')),),
                              ),
                            ),
                            SizedBox(width: 14.w,),
                            Expanded(
                              flex: 7,
                              child: Container(
                                padding: EdgeInsets.symmetric(vertical: 10.w),
                                child: Row(
                                  children: [
                                      Container(
                                        width: 46.w,
                                        height: 46.w,
                                        decoration: BoxDecoration(
                                          borderRadius: BorderRadius.circular(46.r),
                                          color: const Color(0xffEDF5F1)
                                        ),
                                        child: ClipRRect(
                                          borderRadius: BorderRadius.circular(46.r),
                                          child: Image.network(clubPlayersController.clubMemberList[index].imageUrl ?? '', fit: BoxFit.fill,),
                                        ),
                                      ),
                    
                                    SizedBox(width: 8.w,),
                    
                                    Expanded(
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          SizedBox(
                                            child: Row(
                                              children: [
                                                Flexible(
                                                  child: Text(clubPlayersController.clubMemberList[index].nickname ?? '', style: TextStyle(fontSize: 15.sp, fontWeight: FontWeight.w500, color: const Color(0xff333333)), maxLines: 1, overflow: TextOverflow.ellipsis,),
                                                ),
                                                SizedBox(width: 8.w,),
                                                SizedBox(
                                                  width: 16.w,
                                                  height: 16.w,
                                                  child: Image.asset(StrUtils.getSexImage(clubPlayersController.clubMemberList[index].sex ?? 0)),
                                                )
                                              ],
                                            ),
                                          ),
                                          // RichText(
                                          //   maxLines: 1,
                                          //   overflow: TextOverflow.ellipsis,
                                          //   text: TextSpan(
                                          //     children: [
                                          //       TextSpan(
                                          //         text: '范德萨',
                                          //         style: TextStyle(fontSize: 15.sp, fontWeight: FontWeight.w500, color: Colors.black)
                                          //       ),
                                          //       WidgetSpan(
                                          //         child: Container(
                                          //           child: Row(
                                          //             children: [
                                          //               SizedBox(width: 8.w,),
                                          //               SizedBox(
                                          //                 width: 16.w,
                                          //                 height: 16.w,
                                          //                 child: Image.asset(StrUtils.getSexImage(clubPlayersController.clubMemberList[index].sex ?? 0)),
                                          //               )
                                          //             ],
                                          //           ),
                                          //         )
                                          //       )
                                          //     ]
                                          //   )
                                          // ),
                    
                                          SizedBox(height: 4.w,),
                    
                                          SizedBox(
                                            width: 69.w,
                                            height: 18.w,
                                            child: Image.asset(StrUtils.getLevelImage(clubPlayersController.clubMemberList[index].grade ?? 1)),
                                          )
                                        ],
                                      ),
                                    ),
                    
                                    Container(
                                      constraints: BoxConstraints(
                                        maxHeight: 18.w,
                                        maxWidth: 130.w
                                      ),
                                      child: ListView.separated(
                                        itemCount: clubPlayersController.clubMemberList[index].goodPosition?.length ?? 0,
                                        shrinkWrap: true,
                                        scrollDirection: Axis.horizontal,
                                        separatorBuilder: (context, subIndex) {
                                          return SizedBox(width: 4.w,);
                                        },
                                        itemBuilder: (BuildContext context, int subIndex) {
                                          return Container(
                                            decoration: BoxDecoration(
                                              color:  StrUtils.getColorByCode(clubPlayersController.clubMemberList[index].goodPosition?[subIndex].positionSortCode).withOpacity(0.1),
                                              borderRadius: BorderRadius.circular(15.r)
                                            ),
                                            padding: EdgeInsets.symmetric(vertical: 2.w, horizontal: 9.w),
                                            child:  Text(clubPlayersController.clubMemberList[index].goodPosition?[subIndex].subdivisionPosition ?? '', style: TextStyle(fontSize: 10.sp, color: 
                                            StrUtils.getColorByCode(clubPlayersController.clubMemberList[index].goodPosition?[subIndex].positionSortCode))),
                                          );
                                        },
                                      ),
                                    )
                                  ],
                                ),
                              ),
                            )
                          ],
                        ),
                      ),
                    );
                  },
                ),
              );
            }
          );
        }
      ),
    );
  }
}